package com.intelligent.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligent.pojo.ProductionWorkOrders;
import com.intelligent.service.ProductionWorkOrdersService;
import com.intelligent.mapper.ProductionWorkOrdersMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
* @author lenovo
* @description 针对表【production_work_orders(生产工单质表)】的数据库操作Service实现
* @createDate 2025-09-04 10:06:51
*/
@Service
public class ProductionWorkOrdersServiceImpl extends ServiceImpl<ProductionWorkOrdersMapper, ProductionWorkOrders>
    implements ProductionWorkOrdersService{

	@Override
	public IPage<Map<String, Object>> pageQuery(Page<?> page,
	                                           String workOrderNo,
	                                           Integer status,
	                                           String productCode,
	                                           String productName,
	                                           Integer overtimeStatus,
	                                           Date plannedStartBegin,
	                                           Date plannedStartEnd,
	                                           Date plannedCompletionBegin,
	                                           Date plannedCompletionEnd,
												String username) {
		return this.baseMapper.selectPageWithConditions(page, workOrderNo, status, productCode, productName,
			overtimeStatus, plannedStartBegin, plannedStartEnd, plannedCompletionBegin, plannedCompletionEnd,username);
	}

	@Override
	public boolean addProductionWorkOrder(ProductionWorkOrders productionWorkOrders) {
		// 生成工单编号
		String workOrderNo = generateWorkOrderNo();
		productionWorkOrders.setWorkOrderNo(workOrderNo);

		// 设置默认值
		if (productionWorkOrders.getStatus() == null) {
			productionWorkOrders.setStatus(4); // 默认待派工
		}
		if (productionWorkOrders.getOvertimeStatus() == null) {
			productionWorkOrders.setOvertimeStatus(2); // 默认正常进行
		}
		if (productionWorkOrders.getProgress() == null) {
			productionWorkOrders.setProgress("0%"); // 默认进度0%
		}
		if (productionWorkOrders.getDistributionTime() == null) {
			productionWorkOrders.setDistributionTime(new Date()); // 默认当前时间
		}

		int result = this.baseMapper.insertProductionWorkOrder(productionWorkOrders);
		return result > 0;
	}

	@Override
	public boolean updateProductionWorkOrder(ProductionWorkOrders productionWorkOrders) {
		int result = this.baseMapper.updateProductionWorkOrder(productionWorkOrders);
		return result > 0;
	}

	@Override
	public boolean deleteProductionWorkOrderById(Long id) {
		int result = this.baseMapper.deleteByIdCustom(id);
		return result > 0;
	}

	@Override
	public java.util.Map<String, Object> getDetailsById(Long id) {
		return this.baseMapper.selectDetailsById(id);
	}

	/**
	 * 生成工单编号
	 * 格式：WO + yyyyMMdd + 4位序号
	 * @return 工单编号
	 */
	private String generateWorkOrderNo() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String dateStr = sdf.format(new Date());

		// 查询当天最大的工单编号
		String prefix = "WO" + dateStr;
		Long maxId = this.baseMapper.selectMaxIdByPrefix(prefix);

		// 生成序号
		long sequence = (maxId == null ? 0 : maxId) + 1;
		String sequenceStr = String.format("%04d", sequence);

		return prefix + sequenceStr;
	}
}




